package com.example.jack.community.view.login;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jack.community.BaseActivity;
import com.example.jack.community.MainActivity;
import com.example.jack.community.R;
import com.example.jack.community.data.loginbean.User;
import com.example.jack.community.model.login.LoginView;
import com.example.jack.community.persenter.login.LoginPresenterImp;

import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements LoginView {


    //登录界面文字
    @BindView(R.id.text)
    TextView mText;


    //登录按钮
    @BindView(R.id.login_button)
    Button mLoginButton;

    //注册账号
    @BindView(R.id.login_tex_register)
    TextView mLoginTexRegister;

    //忘记密码
    @BindView(R.id.login_tex_forget_password)
    TextView mLoginTexForgetPassword;

    //输入框用户名,用户手机号
    @BindView(R.id.edt_user)
    EditText mEdtUser;

    //输入框用户密码
    @BindView(R.id.edt_password)
    EditText mEdtPassword;

    //用户Pre接口
    private LoginPresenterImp mloginprsenter = new LoginPresenterImp();

    //Log
    private static final String TAG = "TestLog";

    //弹出框对象
    private SweetAlertDialog pDialog;

    @SuppressLint("InlinedApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_activity_login);
        ButterKnife.bind(this);

        judge();                            //判断用户是否已经登录
        type();                            //设置文字字体
        mloginprsenter.attachView(this); //绑定视图
    }

    /**
     * 判断用户是否已经登录
     */
    private void judge() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        String name = sharedPreferences.getString("token", "null");
        String language = sharedPreferences.getString("language", "null");
        if (language.equals("en")) {
            Log.d(TAG, "judge: "+language);
            Resources resources = getResources();
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration config = resources.getConfiguration();
            // 应用用户选择语言
            config.locale = Locale.ENGLISH;
            resources.updateConfiguration(config, dm);
        }
        if (language.equals("zh")) {
            Resources resources = getResources();
            DisplayMetrics dm = resources.getDisplayMetrics();
            Configuration config = resources.getConfiguration();
            // 应用用户选择语言
            config.locale = Locale.TRADITIONAL_CHINESE;
            resources.updateConfiguration(config, dm);
        }
        if (!name.equals("null")) {
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }
    }

    /**
     * 设置文字字体
     */
    private void type() {
        Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Courier.ttf");
        mText.setTypeface(typeface);
    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.login_button, R.id.login_tex_register, R.id.login_tex_forget_password})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_button:
                showdialog();  //提示弹出框
                User user = new User(mEdtUser.getText().toString(), mEdtPassword.getText().toString());
                mloginprsenter.login(user);
                break;
            case R.id.login_tex_register:
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
                break;
            case R.id.login_tex_forget_password:
                startActivity(new Intent(LoginActivity.this, Forget_aActivity.class));
                break;
        }
    }

    /**
     * 提示弹出框
     */
    private void showdialog() {
        pDialog = new SweetAlertDialog(this, SweetAlertDialog.PROGRESS_TYPE);
        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("Loading");
        pDialog.setCancelable(false);
        pDialog.show();
    }

    /**
     * 登录成功回调
     *
     * @param msg
     */
    @Override
    public void loginSuccess(String msg) {
        startActivity(new Intent(LoginActivity.this, MainActivity.class));
        pDialog.cancel();
    }

    /**
     * 登录失败回调
     *
     * @param msg
     */
    @Override
    public void loginFail(String msg) {
        pDialog.cancel();
        new SweetAlertDialog(this, SweetAlertDialog.ERROR_TYPE)
                .setTitleText(msg)
                .setContentText("")
                .show();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(this, "" + msg, Toast.LENGTH_SHORT).show();
        pDialog.cancel();
    }

    /**
     * 存储用户数据
     *
     * @param user
     */
    @Override
    public void Share(User user) {
        SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
        editor.putString("token", user.getTocken());
        editor.putString("username", user.getPhonenum());
        Log.d(TAG, "LoginActivity:  Share: " + user.getPhonenum());
        editor.putString("telephone", user.getPhonenum());
        editor.apply();
        Log.d(TAG, "LoginActivity:  Share: 数据保存成功");
    }
}
